package com.woniuxy.rms.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.rms.advice.LogPoint;
import com.woniuxy.rms.dto.RoleDTO;
import com.woniuxy.rms.pojo.Roles;
import com.woniuxy.rms.service.RoleService;
import com.woniuxy.rms.util.DataResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Auther: ChenGang  <br/>
 * Date: 2020/3/24 0024:上午 11:03  <br/>
 * Description:角色控制层
 */
@Controller
@RequestMapping("/roles")
public class RoleController {

    @Autowired
    RoleService roleService;

    /**
     * Description : 角色列表  <br/>
     * ChangeLog : 1. 创建 (2020/3/24 0024 上午 11:09 [ChenGang]);
     *
     * @return com.woniuxy.rms.util.DataResult
    */
    @GetMapping("/list")
    @ResponseBody
    @LogPoint("查看角色")
    @RequiresPermissions("role:select")
    public DataResult roleList(@RequestParam(defaultValue = "1",required = false,name = "page") Integer currentPage,
                               @RequestParam(defaultValue = "10",required = false,name = "limit") Integer pageSize){
        List<Roles> roles = roleService.findByPage(currentPage,pageSize);
        PageInfo info = new PageInfo(roles);
        DataResult success = DataResult.success(info);
        return success;
    }

    /**
     * Description : 添加角色  <br/>
     * ChangeLog : 1. 创建 (2020/3/24 0024 上午 11:13 [ChenGang]);
     * @param roleDTO
     * @return com.woniuxy.rms.util.DataResult
    */
    @PostMapping("/save")
    @ResponseBody
    @LogPoint("添加角色")
    @RequiresPermissions("role:save")
    public DataResult insert(RoleDTO roleDTO){
        Roles roles = new Roles();
        roles.setRole(roleDTO.getRole());
        roles.setDescription(roleDTO.getDescription());
        String menuId = roleDTO.getId();
        return roleService.insert(roles,menuId);
    }

    /**
     * Description : 删除角色  <br/>
     * ChangeLog : 1. 创建 (2020/3/24 0024 上午 11:15 [ChenGang]);
     * @param id
     * @return com.woniuxy.rms.util.DataResult
    */
    @DeleteMapping("/delete")
    @ResponseBody
    @LogPoint("删除角色")
    @RequiresPermissions("role:delete")
    public DataResult delete(Integer id){
        return roleService.deleteById(id);
    }

    /**
     * Description : 批量删除  <br/>
     * ChangeLog : 1. 创建 (2020/3/24 0024 上午 11:18 [ChenGang]);
     * @param ids
     * @return com.woniuxy.rms.util.DataResult
    */
    @DeleteMapping("/batchDel")
    @ResponseBody
    @RequiresPermissions("role:delete")
    public DataResult batchDel(String ids){
        return roleService.batchDel(ids);
    }

    /**
     * 导航到编辑角色的页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/editRole")
    public String editRole(Integer id, Model model){
        Roles roles = roleService.findById(id);
        model.addAttribute("roles",roles);
        return "system/editRole";
    }
    /**
     * Description : 编辑角色  <br/>
     * ChangeLog : 1. 创建 (2020/3/25 0025 下午 4:14 [Administrator]);
     * @param roleDTO
     * @return com.woniuxy.rms.util.DataResult
    */
    @PutMapping("/updateRole")
    @ResponseBody
    @LogPoint("编辑角色")
    @RequiresPermissions("role:update")
    public DataResult update(RoleDTO roleDTO){
        Roles roles = new Roles();
        roles.setId(roleDTO.getRoleId());
        roles.setRole(roleDTO.getRole());
        roles.setDescription(roleDTO.getDescription());
        String menuId = roleDTO.getId();
        return roleService.update(roles,menuId);
    }
}